<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('指标')" prop="categoriesType">
            <el-select v-model="form.categoriesType" :placeholder="$t('指标')" :disabled="!!form.id" @change="categoriesTypeChange">
              <el-option
                v-for="(dict, index) in categoriesTypeOptions"
                :key="index"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <div class="area-header">
        <span class="area-header-title">{{ $t('内容') }}</span>
      </div>
      <el-table class="nowrapTable formTable" :data="form.dealDdContentList" border>
        <el-table-column :label="$t('内容')" align="center" prop="content">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('内容') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealDdContentList.' + scope.$index + '.content'" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
              <el-input v-model="scope.row.content" type="textarea" :autosize="{ minRows: 2 }" :maxlength="2000" show-word-limit :placeholder="$t('内容')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('排序')" align="center" prop="sort" width="80">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('排序') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'dealDdContentList.' + scope.$index + '.sort'" :rules="[{required: true, message: $t('必填信息'), trigger: ['blur', 'change']}]">
              <el-input-number v-model="scope.row.sort" :controls="false" :precision="0" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" width="80">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="delContent(scope.$index, scope.row)">{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addContent">{{ $t("添加") }}</el-button>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <SaveBtn has-permi="deal:dd" />
    </div>
  </div>
</template>

<script>
  import { saveDealDdList, getDealDdList } from '@/api/deal/spo/red/dealSpoInvestmentRed'
    import '@riophae/vue-treeselect/dist/vue-treeselect.css'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                  dealDdContentList: [],
                  id: undefined
                },
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  categoriesType: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ],
                  categoriesChildType: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ],
                  sort: [
                    { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
                  ]
                },
                // 尽调项树选项
                ddItemOptions: [],
                // 尽调清单大类
                categoriesTypeOptions: []
            }
        },
        created() {
          this.getDicts('dd_categories_type').then(response => {
            this.categoriesTypeOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                  const prams = {
                    id: data.id,
                    dealId: this.$route.query.id
                  }
                    getDealDdList(prams).then(response => {
                        this.form = response.data
                        this.form.id = response.data.id
                        this.categoriesTypeChange(this.form.categoriesType)
                        this.loading = false
                    })
                } else {
                    this.reset()
                  console.log('是算是1', this.form)
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                id: undefined,
                dealId: this.$route.query.id,
                ddItemOptions: [],
                categoriesType: undefined,
                categoriesChildType: undefined,
                dealDdContentList: [],
                createBy: undefined,
                createTime: undefined,
                updateBy: undefined,
                updateTime: undefined,
                delFlag: undefined,
                sort: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        this.form.dealId = this.$route.query.id
                        saveDealDdList(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        }).catch(() => {
                          this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          // 尽调清单大类change
          categoriesTypeChange(val) {
            if (this.form.id === undefined) {
              this.form.categoriesChildType = undefined
            }
            for (let i = 0; i < this.categoriesTypeOptions.length; i++) {
              if (this.categoriesTypeOptions[i].dictValue === '' + val) {
                this.getDicts(this.categoriesTypeOptions[i].childDict).then(response => {
                  this.categoriesTypeChildrenOptions = response.data
                })
                return
              }
            }
          },
          // 删除关键字列表
          delContent(index) {
            this.form.dealDdContentList.splice(index, 1)
          },
          // 添加尽调内容
          addContent() {
            const row = {
              id: undefined,
              content: undefined,
              sort: this.form.dealDdContentList.length + 1
            }
            if (!(this.form.dealDdContentList instanceof Array)) {
              this.$set(this.form, 'dealDdContentList', [])
            }
            this.$nextTick(() => {
              this.form.dealDdContentList.push(row)
            })
          }
        }
    }
</script>
